<?php

namespace app\zyhsapi\controller\v3;

use app\common\lib\Util;
use app\zyhsapi\controller\ZyhsAuth;
use app\zyhsapi\model\ZyhsAppNotice;
use app\zyhsapi\model\ZyhsBanner;
use app\zyhsapi\model\ZyhsConfig;
use app\zyhsapi\model\ZyhsUser;
use app\zyhsapi\model\ZyhsUserQuestion;
use app\zyhsapi\model\ZyhsVrtualuserComment;
use app\zyhsapi\service\CacheZyhs;
use think\Env;

class Index extends ZyhsAuth
{
    /**
     * @throws \RedisException
     */
    public function index()
    {
        $banner = (new ZyhsBanner())->versionBanner();
        $bannerTmp = [];
        foreach ($banner as $value)
        {
            $bannerTmp[] = [
                'name' => $value['name'],
                'img' => $value['image'],
                'target' => $value['target'],
                'link' => $value['link'],
            ];
        }
        //登录后,注册用户赠送连麦卡券后弹出弹窗
        if ($this->user && CacheZyhs::get('reg_user_send_coupon_' . $this->user['id']))
        {
            CacheZyhs::rm('reg_user_send_coupon_' . $this->user['id']);
            $notice = (new ZyhsAppNotice())->getInfoById(1);
        } else
        {
            $notice = (new ZyhsAppNotice())->getNewestNotice();
        }
        $zoneList = config('consult_zone');
        $zoneListTmp = [];
        foreach ($zoneList as $zone)
        {
            if ($zone['status'])
            {
                if ($zone['type'] === 5 && Util::IsHuaweiOrHonor())//华为渠道不展示婚姻关系专区
                {

                } else
                {
                    unset($zone['type'], $zone['name'], $zone['status'], $zone['extend']);
                    $zoneListTmp[] = $zone;
                }
            }
        }

        if ((Util::IsIos() && Util::CheckAppVersion('1.1.1') === '小') || Util::CheckAppVersion('1.1.0') === '小')
        {
            $askList = (new ZyhsVrtualuserComment())->commentList(['id', 'question']);
            $askListTmp = [];
            foreach ($askList as $value)
            {
                $askListTmp[] = [
                    'id' => $value['id'],
                    'title' => $value['question'],
                ];
            }
            $askListTmp = array_slice($askListTmp, 0, 3);
        } else
        {
            $zyhsUserQuestionModel = new ZyhsUserQuestion();
            $testAccountConfig = (new ZyhsConfig())->configInfo('rtc');
            if (Util::ZyhsIsHuaweiOrHonorHg())
            {
                if (Util::IsHuawei())
                {
                    $idList = $testAccountConfig['huawei_hg_ask_list_ids'] ? explode(',', $testAccountConfig['huawei_hg_ask_list_ids']) : [];
                } else
                {
                    $idList = $testAccountConfig['honor_hg_ask_list_ids'] ? explode(',', $testAccountConfig['honor_hg_ask_list_ids']) : [];
                }
                $total = count($idList);
                $pageDataIds = [
                    'total' => $total,// 记录总数
                    'per_page' => $total,// 每页多少条
                    'current_page' => 1,// 当前页数
                    'last_page' => 1,// 总页数
                    'data' => [],
                ];
                foreach ($idList as $idItem)
                {
                    $pageDataIds['data'][$idItem] = $idItem;
                }
            } else
            {
                $pageDataIds = $zyhsUserQuestionModel->getQuestionTimeSortIds(1, 20);
            }

            $questionDataTmp = [];
            $userModel = new ZyhsUser();
            $num = count($pageDataIds['data']);
            //用户问题小于两条显示假数据
            if ($num > 2)
            {
                $testUids = explode(',', $testAccountConfig['zyhsapp_test_user_id']);
                $ids = array_keys($pageDataIds['data']);
                $questionData = $zyhsUserQuestionModel->batchGetInfoByIds($ids, ['id', 'question', 'is_hide', 'uid']);
                foreach ($pageDataIds['data'] as $key => $sort)
                {
                    $questionItem = $questionData[$key] ?? null;
                    if (!$questionItem || in_array($questionItem['uid'], $testUids, false))
                    {   // 问题获取异常 或者 测试用户发送的问题隐藏
                        continue;
                    }
                    if ($questionItem && $questionItem['is_hide'] === 0)
                    {
                        $avatar = $userModel->getInfoById($questionItem['uid'], ['avatar'])['avatar'];
                        $questionDataTmp[] = [
                            'id' => $questionItem['id'],
                            'question' => $questionItem['question'],
                            'avatar' => $avatar,
                        ];
                    }
                }
            } else
            {
                $askList = (new ZyhsVrtualuserComment())->commentList(['id', 'question']);
                foreach ($askList as $value)
                {
                    $questionDataTmp[] = [
                        'id' => $value['id'],
                        'question' => $value['question'],
                        'avatar' => 'zyhsapp\/uploads\/avatar\/20231228\/10651d8192f0b55759f8e0d2fd78e4f9.jpg',
                    ];
                }
                $questionDataTmp = array_slice($questionDataTmp, 0, 3);
            }
        }

        if (Util::IsHuaweiOrHonor())
        {
            $divinationQuestionLink = 'divination_question?type=0&name1=%E5%8D%A0%E6%98%9F%E9%AA%B0%E5%AD%90&name2=%E5%A1%94%E7%BD%97%E5%8D%A0%E5%8D%9C&need_auth=0';
        } else
        {
            $divinationQuestionLink = 'divination_question?type=2&name1=%E5%8D%A0%E6%98%9F%E9%AA%B0%E5%AD%90&name2=%E5%A1%94%E7%BD%97%E5%8D%A0%E5%8D%9C&need_auth=0';
        }
        $buttons = [
            [
                'name' => '星图',
                'icon' => '/zyhsapp/buttons/astrolable.png',
                'target' => 'local',
                'link' => 'astrolable?need_auth=1',
            ], [
                'name' => '合盘',
                'icon' => '/zyhsapp/buttons/composite.png',
                'target' => 'local',
                'link' => 'composite?need_auth=1',
            ], [
                'name' => '解问题',
                'icon' => '/zyhsapp/buttons/divination_question.png',
                'target' => 'local',
                'link' => $divinationQuestionLink,
            ], [
                'name' => '星座解读',
                'icon' => '/zyhsapp/buttons/astrolable_detail.png',
                'target' => 'link',
                'link' => Env::get('host.domain') . "/zyhsweb/astro_explain_new/index.html?rid={$this->user['rid']}&need_auth=1",
            ],
        ];
        if (!Util::IsIos())
        {   // 不是 ios
            unset($buttons[1]);
            $buttons = array_values($buttons);
        }

        return show(config('code.success'), 'Success.', [
            'banner_list' => $bannerTmp,
            'consult_zone' => $zoneListTmp,
            'ask_list' => $askListTmp ?? [],
            'notice' => $notice ?: null,
            'question_answer_list' => $questionDataTmp ?? [],
            'buttons' => $buttons,
        ]);
    }
}
